package com.gzx.plugin.tjzy.modular.project.belong.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.tjzy.modular.project.belong.entity.GzxProjectBelong;
import com.gzx.plugin.tjzy.modular.project.belong.entity.vo.GzxBelongUserVo;
import com.gzx.plugin.tjzy.modular.project.belong.entity.vo.GzxProjectBelongVo;
import com.gzx.plugin.tjzy.modular.project.belong.mapper.GzxProjectBelongMapper;
import com.gzx.plugin.tjzy.modular.project.belong.service.GzxProjectBelongService;
import com.gzx.plugin.tjzy.modular.project.entity.bo.GzxProjectBo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目关联子表
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class GzxProjectBelongServiceImpl implements GzxProjectBelongService, InitializingBean {

    private final GzxProjectBelongMapper baseMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO document why this method is empty
    }

    /**
     * 插入项目关联子表
     */
    @Override
    @DS("tjzy")
    public void insertBelong(GzxProjectBo bo) {
        if (ObjectUtils.isEmpty(bo.getBelongTeam()) || ObjectUtils.isEmpty(bo.getBelongDirector())) {
            log.warn("项目关联子表数据为空 {} ", bo);
            return;
        }
        String[] belongTeam = bo.getBelongTeam().split(",");
        String[] belongDirector = bo.getBelongDirector().split(",");
        List<GzxProjectBelong> list = new ArrayList<>();
        for (String string : belongDirector) {
            list.add(new GzxProjectBelong(bo.getProjectCode(), string, "1"));
        }
        for (String string : belongTeam) {
            list.add(new GzxProjectBelong(bo.getProjectCode(), string, "2"));
        }
        baseMapper.insertBatch(list);
    }

    /**
     * 根据项目code获取关联表信息
     */
    @Override
    @DS("tjzy")
    public GzxBelongUserVo queryByCode(String projectCode) {
        GzxBelongUserVo belong = new GzxBelongUserVo();
        //查询所属主管
        List<GzxProjectBelongVo> belongDirector = baseMapper.selectVoList(Wrappers.<GzxProjectBelong>lambdaQuery()
                .eq(GzxProjectBelong::getProjectCode, projectCode));
        belong.setBelongDirector(belongDirector);
        //查询所属小组
        List<GzxProjectBelongVo> belongTeam = baseMapper.selectVoList(Wrappers.<GzxProjectBelong>lambdaQuery()
                .eq(GzxProjectBelong::getProjectCode, projectCode));
        belong.setBelongTeam(belongTeam);
        return belong;
    }

    /**
     * 修改项目关联子表
     */
    @Override
    @DS("tjzy")
    public void updateBelong(GzxProjectBo bo) {
        //先删再增加
        baseMapper.delete(new LambdaUpdateWrapper<GzxProjectBelong>().set(GzxProjectBelong::getDeleteFlag, "DELETE")
                .eq(GzxProjectBelong::getProjectCode, bo.getProjectCode()));
        insertBelong(bo);
    }
}
